//
//  YDYPresentManager.m
//  DY_Weibo3
//
//  Created by 杨德勇 on 2016/8/18.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYPresentManager.h"
#import "YDYPresentionController.h"
@interface YDYPresentManager()

@end

@implementation YDYPresentManager

#pragma mark -
#pragma mark -转场动画代理
//1.这个方法返回一个后对象，负责管理转场动画出现后的2个界面，可以修改出现后的尺寸
-(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    
    YDYPresentionController *presentController = [[YDYPresentionController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
    presentController.presentFrame = self.presentFrame;
    
    
    
    return presentController;
    
}
//2.这个方法返回一个对象，这个对象里面实现了动画出现协议
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    [[NSNotificationCenter defaultCenter]postNotificationName:YDYPOPViewDidShowNotification object:nil];
    
    self.presenting = YES;
    return self;
}
//3.同上，动画出现变成动画消失
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    [[NSNotificationCenter defaultCenter]postNotificationName:YDYPOPViewDidDismissedNotification object:nil];
    
    self.presenting = NO;
    return self;
}

#pragma mark -
#pragma mark -UIViewControllerAnimatedTransitioning
//转场动画在这里完成
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.5;
}
//动画对象，由于动画代理是这个控制器，出现和消失都写了self，所以这个方法控制出现和消失，这个方法重写了，系统modal默认的从下往上出现的动画消失。而且自己把需要展示的视图放进容器视图里面
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    
    if (self.isPresenting) {
        [self willPresent:transitionContext];
        
    }else{
        [self willDismiss:transitionContext];
    }
}

#pragma mark -
#pragma mark -负责动画出现
-(void)willPresent:(id <UIViewControllerContextTransitioning>)transitionContext{
    
    UIView *toview = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    [transitionContext.containerView addSubview:toview];
    
    toview.transform = CGAffineTransformMakeScale(1.0, 0.0);
    
    //修改锚点，让动画不在中间出现(锚点只能0 - 1)
    toview.layer.anchorPoint = CGPointMake(0.5, 0);
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        toview.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        //注意：一定要告诉系统动画结束，不然有些功能会出错
        [transitionContext completeTransition:YES];
    }];
}

#pragma mark -
#pragma mark -负责动画停止
-(void)willDismiss:(id <UIViewControllerContextTransitioning>)transitionContext{

    UIView*formView =  [transitionContext viewForKey:UITransitionContextFromViewKey];
    
    formView.layer.anchorPoint = CGPointMake(0.5, 0);
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        formView.transform = CGAffineTransformMakeScale(1.0, 0.00001);
    } completion:^(BOOL finished) {
        //注意：一定要告诉系统动画结束，不然有些功能会出错
        [transitionContext completeTransition:YES];
    }];
    
}
@end
